//
//  PrescriptionOrderPayResultSuccessView.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2023/1/10.
//  Copyright © 2023 QDSG. All rights reserved.
//

import UIKit

class PrescriptionOrderPayResultSuccessView: UIView {
    var model: PrescriptionOrderDetail? {
        didSet {
            let price = NSMutableAttributedString(string: "¥\(model?.prescriptionAmount ?? "")", attributes: [.font: UIFont.ding(16), .foregroundColor: UIColor.init(hex: "#F54C4C")])
            price.addAttributes([.font: UIFont.regular_12], range: NSMakeRange(0, 1))
            dataSource = [
                .init(title: "费用明细", items: model?.westernNameDtoList?.map({
                    PrescriptionOrderPayResultSuccessItem(title: $0.name, number: $0.drugNumber, price: $0.outPrice)
                }) ?? []),
                .init(title: "费用总额", describeAttri: price, items: [
                    .init(title: "医保账号支付", price: model?.recPayAmout),
                    .init(title: "其他抵扣金额", price: model?.discountAmout),
                    .init(title: "现金账户", price: model?.totalAmount)
                ])
            ]
            tableView.reloadData()
            let height = 40 * 2 + 30 * 3 + 30 * min(model?.westernNameDtoList?.count ?? 0, 4)
            tableView.snp.updateConstraints { make in
                make.height.equalTo(height)
            }
        }
    }
    lazy var tableView = CLTableView(delegate: self, cells: [PrescriptionOrderPayResultSuccessTableViewCell.self])
    var dataSource: [PrescriptionOrderPayResultSuccessSection] = []
    init() {
        super.init(frame: .zero)
        
        layer.cornerRadius = 10
        layer.masksToBounds = true
        backgroundColor = .white
        
        addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            make.height.equalTo(100)
        }
        tableView.register(headerFooters: [PrescriptionOrderPayResultSuccessTableViewHeaderView.self])
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension PrescriptionOrderPayResultSuccessView: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        dataSource.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource[section].items.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if let cell = tableView.dequeueReusableCell(withIdentifier: "\(PrescriptionOrderPayResultSuccessTableViewCell.self)") as? PrescriptionOrderPayResultSuccessTableViewCell {
            cell.model = dataSource[indexPath.section].items[indexPath.row]
            return cell
        }
        return UITableViewCell()
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "\(PrescriptionOrderPayResultSuccessTableViewHeaderView.self)") as? PrescriptionOrderPayResultSuccessTableViewHeaderView
        header?.model = dataSource[section]
        return header
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        40
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        30
    }
//    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
//        return 1
//    }
}

struct PrescriptionOrderPayResultSuccessSection {
    var title: String?
    var describe: String?
    var describeAttri: NSAttributedString?
    var items: [PrescriptionOrderPayResultSuccessItem] = []
}

struct PrescriptionOrderPayResultSuccessItem {
    var title: String?
    var number: String?
    var price: String?
}

class PrescriptionOrderPayResultSuccessTableViewCell: UITableViewCell {
    
    var model: PrescriptionOrderPayResultSuccessItem? {
        didSet {
            title.text = model?.title
            number.text = model?.number == nil ? "" : "¥\(model?.number ?? "")"
            let attri = NSMutableAttributedString(string: "×\(model?.price ?? "")", attributes: [.font: UIFont.ding(14), .foregroundColor: UIColor.init(hex: "#333333")])
            attri.addAttributes([.font: UIFont.regular(10)], range: NSMakeRange(0, 1))
            price.attributedText = attri
        }
    }
    
    let title = UILabel()
    let number = UILabel()
    let price = UILabel()
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        
        contentView.addSubview(title)
        title.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.centerY.equalToSuperview()
        }
        title.font = .regular_14
        title.textColor = .init(hex: "#333333")
        
        contentView.addSubview(number)
        number.snp.makeConstraints { make in
            make.left.equalTo(150)
            make.centerY.equalToSuperview()
        }
        number.font = .regular(10)
        number.textColor = .init(hex: "#999999")
        
        contentView.addSubview(price)
        price.snp.makeConstraints { make in
            make.right.equalTo(-16)
            make.centerY.equalToSuperview()
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class PrescriptionOrderPayResultSuccessTableViewHeaderView: UITableViewHeaderFooterView {
    
    var model: PrescriptionOrderPayResultSuccessSection? {
        didSet {
            title.text = model?.title
            if let describeAttri = model?.describeAttri {
                describe.attributedText = describeAttri
            } else {
                describe.text = model?.describe
            }
        }
    }
    
    let title = UILabel()
    let describe = UILabel()
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        
        contentView.addSubview(title)
        title.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.top.equalTo(10)
        }
        title.font = .medium_16
        title.textColor = .black
        title.text = " "
        contentView.layoutIfNeeded()
        
        contentView.addSubview(describe)
        describe.snp.makeConstraints { make in
            make.right.equalTo(-21)
            make.centerY.equalTo(title)
        }
        describe.font = .regular_12
        describe.textColor = .init(hex: "#FF3643")
        
        let bgLayer1 = CAGradientLayer()
        bgLayer1.colors = [UIColor.init(hex: "#55B8A9FF").cgColor, UIColor.init(hex: "#55B8A900").cgColor]
        bgLayer1.locations = [0, 1]
        bgLayer1.frame = CGRect(x: title.frame.origin.x, y: title.frame.maxY - 10, width: 70, height: 6)
        bgLayer1.cornerRadius = 3
        bgLayer1.masksToBounds = true
        bgLayer1.startPoint = CGPoint(x: 0, y: 0.5)
        bgLayer1.endPoint = CGPoint(x: 1, y: 0.5)
        contentView.layer.insertSublayer(bgLayer1, at: 0)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
